#include "StdAfx.h"
#include "..\engine\Ammo.hpp"

namespace he
{
	Ammo::Ammo(std::string const &Type, float Weight, float Velocity, unsigned short Penetration100, unsigned short Penetration500, unsigned short Penetration1000, unsigned short Penetration1500, unsigned short Penetration2000)
		: m_Type(Type), m_Weight(Weight), m_Velocity(Velocity)
	{
		m_PenetrationTable[0] = Penetration100;
		m_PenetrationTable[1] = Penetration100;
		m_PenetrationTable[2] = Penetration500;
		m_PenetrationTable[3] = Penetration1000;
		m_PenetrationTable[4] = Penetration1500;
		m_PenetrationTable[5] = Penetration2000;
		m_PenetrationTable[6] = 0;
	}

	unsigned short Ammo::get_penetration(unsigned short Range) const
	{
		// Over 2 km, 0 penetration
		if(Range > 2000)
			return 0;
		// Get range
		RangePair_t const range_interval = get_nearest_range(Range);

		return (get_penetration_value(range_interval.first) + (Range - range_interval.first) * ((get_penetration_value(range_interval.second) - get_penetration_value(range_interval.first)) / (range_interval.second - range_interval.first)));
	}

	Ammo::RangePair_t const Ammo::get_nearest_range(unsigned short Range) const
	{
		if(Range < 100)
			return RangePair_t(0, 100);
		else if(Range >= 100 && Range < 500)
			return RangePair_t(100, 500);
		else if(Range >= 500 && Range < 1000)
			return RangePair_t(500, 1000);
		else if(Range >= 1000 && Range < 1500)
			return RangePair_t(1000, 1500);
		else if(Range >= 1500 && Range < 2000)
			return RangePair_t(1500, 2000);
		else
			return RangePair_t(2000, std::numeric_limits<unsigned short>::max());
	}

	unsigned short Ammo::get_penetration_value(unsigned short Range) const
	{
		switch(Range)
		{
		case 0:
			return m_PenetrationTable[0];
			break;
		case 100:
			return m_PenetrationTable[1];
			break;
		case 500:
			return m_PenetrationTable[2];
			break;
		case 1000:
			return m_PenetrationTable[3];
			break;
		case 1500:
			return m_PenetrationTable[4];
			break;
		case 2000:
			return m_PenetrationTable[5];
			break;
		default:
			return m_PenetrationTable[6];
			break;
		}
	}
}